package com.net.xpay.core.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.enums.invoice.InvoiceType;
import com.net.common.exception.BusinessException;
import com.net.common.util.PhoneUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.InvoiceTitleModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**发票抬头验证
 * @author 86188
 */
@Component
@Slf4j
public class InvoiceTitleValidator {

    public void validateInvoiceTitle(InvoiceTitleModel model) {
        log.info("validateInvoiceTitle model={}", SerializationUtil.obj2String(model));
        if (InvoiceType.VAT_SPECIAL_INVOICE.equals(model.getInvoiceType())) {
            validateSpecialInvoiceTitle(model);
        } else if (InvoiceType.VAT_INVOICE.equals(model.getInvoiceType())) {
            validateNormalInvoiceTitle(model);
        } else {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票类型错误");
        }
    }

    public void validateSpecialInvoiceTitle(InvoiceTitleModel model) {
        if (model.getInvoiceTitleType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "抬头类型不能为空");
        }
        if (model.getInvoiceType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票类型不能为空");
        }
        if (Strings.isNullOrEmpty(model.getInvoiceTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能为空");
        }
        if (model.getInvoiceTitle().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getTaxIdNumber())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能为空");
        }
        if (model.getTaxIdNumber().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getAccountsBank())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户银行不能为空");
        }
        if (model.getAccountsBank().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户银行不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getAccountNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户账号不能为空");
        }
        if (model.getAccountNo().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户账户不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getRegisteredAddress())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业注册地址不能为空");
        }
        if (model.getRegisteredAddress().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业注册地址不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getRegisteredPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业注册电话不能为空");
        }
        if (!PhoneUtil.isEffectiveMobile(model.getRegisteredPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
    }

    public void validateNormalInvoiceTitle(InvoiceTitleModel model) {
        if (model.getInvoiceTitleType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "抬头类型不能为空");
        }
        if (model.getInvoiceType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票类型不能为空");
        }
        if (Strings.isNullOrEmpty(model.getInvoiceTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能为空");
        }
        if (model.getInvoiceTitle().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能超过128字");
        }
        if (Strings.isNullOrEmpty(model.getTaxIdNumber())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能为空");
        }
        if (model.getTaxIdNumber().length() > 128){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能超过128字");
        }

    }
}
